package com.hongkzh.www.look.enterprise.watch.view.activity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.hongkzh.www.R;
import com.hongkzh.www.look.enterprise.watch.model.bean.AdvByIdBean;
import com.hongkzh.www.look.enterprise.watch.model.bean.AdvRecommendByAdvIdBean;
import com.hongkzh.www.look.enterprise.userinfo.model.bean.MediaPhotoBean;
import com.hongkzh.www.look.enterprise.userinfo.model.bean.MediaProductBean;
import com.hongkzh.www.look.enterprise.watch.presenter.WatchPresenter;
import com.hongkzh.www.view.activity.BaseActivity;
import com.hongkzh.www.look.enterprise.watch.view.adapter.WatRvAdapter;
import com.hongkzh.www.view.customview.MyFooter;
import com.hongkzh.www.look.enterprise.watch.view.iview.WatchIView;
import com.liaoinstan.springview.widget.SpringView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 看视频
 */
public class WatchActivity extends BaseActivity<WatchPresenter> implements WatchIView, SpringView.OnFreshListener {

    @BindView(R.id.watch_rv)
    RecyclerView watchRv;
    @BindView(R.id.wat_sv)
    SpringView watSv;
    private WatRvAdapter watRvAdapter;
    private MyFooter myFooter;
    private String uid;
    private boolean lastPage;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_watch;
    }

    @Override
    protected void init() {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setTranslucent(R.color.color_D8D8D8);

        Intent intent = getIntent();
        uid = intent.getStringExtra("uid");
        String id = intent.getStringExtra("id");
        setPresenter(new WatchPresenter());
        getPresenter().getAdvById(id)
                .getAdvRecommendByAdvId(id)
                .getAllAlbumByUserId(uid, 1)
                .getAllProductByUserId(uid);

        myFooter = new MyFooter(this);
        watSv.setFooter(myFooter);

        watRvAdapter = new WatRvAdapter(this);
        watchRv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        watchRv.setAdapter(watRvAdapter);
    }

    @Override
    protected void setListener() {
        watSv.setListener(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onBackPressed() {
        if (watRvAdapter.onBack()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        watRvAdapter.onVideoPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        watRvAdapter.onVideoResume();
    }

    @Override
    public void onError(Exception e) {

    }

    @Override
    public void setAdvById(AdvByIdBean advByIdBean) {
        watRvAdapter.addAdvById(advByIdBean);
        watRvAdapter.notifyItemChanged(0);
    }

    @Override
    public void setAllAlbumByUserId(MediaPhotoBean mediaPhotoBean) {
        watRvAdapter.addAllAlbumByUserId(mediaPhotoBean);
        watRvAdapter.notifyItemChanged(2);
    }

    @Override
    public void setAllProductByUserId(MediaProductBean mediaProductBean) {
        watRvAdapter.addAllProductByUserId(mediaProductBean);
        watSv.onFinishFreshAndLoad();
    }

    @Override
    public void onLastPage(boolean lastPage) {
        this.lastPage = lastPage;
        myFooter.setLastPage(lastPage);
    }

    @Override
    public void setAdvRecommendByAdvId(AdvRecommendByAdvIdBean advRecommendByAdvIdBean) {
        watRvAdapter.addAdvRecommendByAdvId(advRecommendByAdvIdBean);
        watRvAdapter.notifyItemChanged(0);
    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onLoadmore() {
        if (lastPage) watSv.onFinishFreshAndLoad();
        else getPresenter().loadmore(uid);
    }
}
